package com.wgts.wgts.mvp.view.activity.shopkeeper;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.toast.MoHuToast;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerAddPutForwardActivityComponent;
import com.wgts.wgts.dagger.module.AddPutForwardActivityMoudule;
import com.wgts.wgts.mvp.presenter.IAddPutForwardActivityPresenter;
import com.wgts.wgts.mvp.view.IAddPutForwardActivityView;
import com.wgts.wgts.tools.AppInfor;

import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddPutForwardActivity extends BaseActivity implements IAddPutForwardActivityView {


    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.yv_title)
    TextView yvTitle;
    @BindView(R.id.linear_zhifu_title)
    LinearLayout linearZhifuTitle;
    @BindView(R.id.tv_zhifu_card)
    TextView tvZhifuCard;
    @BindView(R.id.edit_zhifu_number)
    EditText editZhifuNumber;
    @BindView(R.id.tv_zhifu_phone)
    TextView tvZhifuPhone;
    @BindView(R.id.tv_zhifu_phone_number)
    TextView tvZhifuPhoneNumber;
    @BindView(R.id.tv_zhifu_code)
    TextView tvZhifuCode;
    @BindView(R.id.tv_zhifu_send_code)
    TextView tvZhifuSendCode;
    @BindView(R.id.edit_zhifu_code)
    EditText editZhifuCode;
    @BindView(R.id.linear_zhifu_model)
    LinearLayout linearZhifuModel;
    @BindView(R.id.linear_ensure)
    LinearLayout linearEnsure;


    @Inject
    IAddPutForwardActivityPresenter presenter;
    @BindView(R.id.tv_zhifu_userName)
    TextView tvZhifuUserName;
    @BindView(R.id.edit_zhifu_userName)
    EditText editZhifuUserName;


    @BindView(R.id.linear_select_zhifu)
    LinearLayout linearSelectZhifu;
    @BindView(R.id.linear_bank_title)
    LinearLayout linearBankTitle;
    @BindView(R.id.linear_select_bank)
    LinearLayout linearSelectBank;
    @BindView(R.id.tv_bank_name)
    TextView tvBankName;
    @BindView(R.id.edit_bank_name)
    TextView editBankName;
    @BindView(R.id.linear_choese_bank)
    RelativeLayout linearChoeseBank;
    @BindView(R.id.tv_bank_opne_name)
    TextView tvBankOpneName;
    @BindView(R.id.edit_bank_opne_name)
    EditText editBankOpneName;
    @BindView(R.id.tv_bank_userName)
    TextView tvBankUserName;
    @BindView(R.id.edit_bank_userName)
    EditText editBankUserName;
    @BindView(R.id.tv_bank_card)
    TextView tvBankCard;
    @BindView(R.id.edit_bank_number)
    EditText editBankNumber;
    @BindView(R.id.tv_bank_phone)
    TextView tvBankPhone;
    @BindView(R.id.tv_bank_phone_number)
    TextView tvBankPhoneNumber;
    @BindView(R.id.tv_bank_code)
    TextView tvBankCode;
    @BindView(R.id.tv_bank_send_code)
    TextView tvBankSendCode;
    @BindView(R.id.edit_bank_code)
    EditText editBankCode;
    @BindView(R.id.linear_bank_model)
    LinearLayout linearBankModel;

    private int selectType = 1;

    private List<String> bankList;

    public static final String[] banks = new String[]{
            "中国银行","中国工商银行","中国建设银行","中国农业银行","邮政储蓄银行","交通银行", "招商银行"
    };


    @Override
    public void init() {
        DaggerAddPutForwardActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .addPutForwardActivityMoudule(new AddPutForwardActivityMoudule(this))
                .build()
                .inject(this);
        bankList = Arrays.asList(banks);
    }

    @Override
    public void onCreate(Bundle saveInstanceBundle) {
        super.onCreate(saveInstanceBundle);
        setContentView(R.layout.add_put_forward_activity);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        selectType = 1;
        tvZhifuPhoneNumber.setText(AppInfor.getInstance().getUserPhone());
        tvBankPhoneNumber.setText(AppInfor.getInstance().getUserPhone());
        linearZhifuModel.setVisibility(View.VISIBLE);
        linearBankModel.setVisibility(View.GONE);
        linearSelectZhifu.setVisibility(View.VISIBLE);
        linearSelectBank.setVisibility(View.INVISIBLE);

    }

    @OnClick({R.id.tv_zhifu_send_code, R.id.linear_ensure, R.id.img_back, R.id.releative_select_zhifubao, R.id.releative_select_bank, R.id.linear_choese_bank,R.id.tv_bank_send_code})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_zhifu_send_code:
                sendSms();
                break;

            case R.id.linear_ensure:
                ensure();
                break;
            case R.id.img_back:
                finish();
                break;

            case R.id.releative_select_zhifubao:
                linearZhifuModel.setVisibility(View.VISIBLE);
                linearBankModel.setVisibility(View.GONE);
                linearSelectZhifu.setVisibility(View.VISIBLE);
                linearSelectBank.setVisibility(View.INVISIBLE);
                selectType = 1;
                break;

            case R.id.releative_select_bank:
                linearZhifuModel.setVisibility(View.GONE);
                linearBankModel.setVisibility(View.VISIBLE);
                linearSelectBank.setVisibility(View.VISIBLE);
                linearSelectZhifu.setVisibility(View.INVISIBLE);
                selectType = 0;
                break;

            case R.id.linear_choese_bank:
                chooseBank();
                break;
            case R.id.tv_bank_send_code:
                sendSms();
                break;
        }
    }

    private void ensure() {
        String bankName;
        String carCode;
        String verifyCode;
        String withdrawType;
        String userName;
        String bankOpenName;
        bankName = "";
        bankOpenName = "";

        if(selectType == 1){
            carCode = editZhifuNumber.getEditableText().toString();
            verifyCode = editZhifuCode.getEditableText().toString();
            userName = editZhifuUserName.getEditableText().toString();
            withdrawType = "1";

            if (carCode.trim().equals("")) {
                MoHuToast.showToast("支付宝账号不能为空!");
                return;
            }

            if (userName.trim().equals("")) {
                MoHuToast.showToast("请输入支付宝绑定的真实姓名");
                return;
            }

            if (verifyCode.trim().length() != 4) {
                MoHuToast.showToast("请输入正确的验证码");
                return;
            }

        }else{
            carCode = editBankNumber.getEditableText().toString();
            verifyCode = editBankCode.getEditableText().toString();
            userName = editBankUserName.getEditableText().toString();
            bankOpenName = editBankOpneName.getEditableText().toString();
            bankName = editBankName.getText().toString();
            withdrawType = "0";

            if(bankName.trim().equals("")){
                MoHuToast.showToast("请选择银行");
                return;
            }

            if(bankOpenName.trim().equals("")){
                MoHuToast.showToast("请输入开户行名称");
                return;
            }

            if (userName.trim().equals("")) {
                MoHuToast.showToast("请输入银行卡绑定的真实姓名");
                return;
            }

            if (carCode.trim().equals("")) {
                MoHuToast.showToast("请输入银行卡卡号!");
                return;
            }


            if (verifyCode.trim().length() != 4) {
                MoHuToast.showToast("请输入正确的验证码");
                return;
            }
        }

        showDialog();
        presenter.addPutForward(bankName, carCode, verifyCode, withdrawType, userName, bankOpenName);
    }

    private void sendSms() {
        tvZhifuSendCode.setEnabled(false);
        tvBankSendCode.setEnabled(false);
        tvZhifuSendCode.setText("正在发送");
        tvBankSendCode.setText("正在发送");
        presenter.sendSms(AppInfor.getInstance().getUserPhone(), "WITHDRAW");

    }


    @Override
    public void countDownTimeOver() {
        tvZhifuSendCode.setEnabled(true);
        tvZhifuSendCode.setText("重新获取");
        tvBankSendCode.setEnabled(true);
        tvBankSendCode.setText("重新获取");
        presenter.removeCountDownTimeListener();
    }

    @Override
    public void countDowmTime(int time) {
        tvZhifuSendCode.setText(String.valueOf(time) + "s");
        tvBankSendCode.setText(String.valueOf(time) + "s");

    }

    @Override
    public void sendSmsResult() {
        presenter.addCountDownTimeListener();
        MoHuToast.showToast("发送成功");
    }

    @Override
    public void putForwardResult() {
        dialogDismiss();
        MoHuToast.showToast("添加成功");
        finish();
    }

    @Override
    public void onError(int status, String msg) {
        if (status == 0x111) {
            tvZhifuSendCode.setEnabled(true);
            tvBankSendCode.setEnabled(true);
            tvZhifuSendCode.setText("发送验证码");
            tvBankSendCode.setText("发送验证码");
        }
        MoHuToast.showToast(msg);
        dialogDismiss();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.removeCountDownTimeListener();
    }

    //条件选择器
    private void chooseBank(){
        OptionsPickerView pvOptions = new OptionsPickerBuilder(AddPutForwardActivity.this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3 ,View v) {
                editBankName.setText(bankList.get(options1));
            }
        })
                .setSubmitText("确认")
                .setCancelText("取消")
                .setTitleText("选择银行")
                .isCenterLabel(true)
                .build();
        pvOptions.setPicker(bankList);
        pvOptions.show();
    }

}
